package cn.getech.data.development.service.impl;

import cn.getech.data.development.constant.Constant;
import cn.getech.data.development.entity.RealTimeTaskLog;
import cn.getech.data.development.mapper.RealTimeTaskLogMapper;
import cn.getech.data.development.model.vo.RealTimeTaskLogSearchVO;
import cn.getech.data.development.model.vo.RealTimeTaskLogVO;
import cn.getech.data.development.service.RealTimeTaskLogService;
import cn.getech.data.intelligence.common.utils.PageUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class RealTimeTaskLogServiceImpl extends ServiceImpl<RealTimeTaskLogMapper, RealTimeTaskLog> implements RealTimeTaskLogService {

    @Resource
    private RealTimeTaskLogMapper mapper;

    @Override
    public R logPage(RealTimeTaskLogSearchVO params) {
        try {
            Page<RealTimeTaskLogVO> page = new Page<>(params.getPage(), params.getLimit());
            if (params.getStartTime() != null) {
                params.setStartTime(params.getStartTime() + " 00:00:00");
            }
            if (params.getEndTime() != null) {
                params.setEndTime(params.getEndTime() + " 59:59:59");
            }
            IPage<RealTimeTaskLogVO> iPage = mapper.logPage(page, params);
            if (CollectionUtil.isNotEmpty(iPage.getRecords())) {
                for (RealTimeTaskLogVO record : iPage.getRecords()) {
                    String msg = record.getOperationPerName() + Constant.RealTimeTaskLogType.valueOfCode(record.getType());
                    record.setDescription(msg + "了该任务。");
                }
            }
            return R.okWithPage(new PageUtils(iPage));
        }catch (Exception e){
            log.error("获取实时任务日志出错！", e);
        }
        return R.okWithPage(new Page<>());
    }
}
